package com.voice.call.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import voice.voice.call.cache.ConfigSystemCache;

import com.voice.call.common.Const;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.entity.GlobalConfig;
import com.voice.call.entity.SupportMcc;
import com.voice.call.entity.UserInfo;
import com.voice.call.mapper.GlobalConfigMapper;
import com.voice.call.service.GlobalConfigService;
import com.voice.call.service.InviteCodeService;
import com.voice.call.service.SupportMccService;
import com.voice.call.service.TaskIntegralService;
import com.voice.call.service.UserInfoService;
import com.voice.call.utils.JSONUtil;
import com.voice.call.utils.StringUtil;

@Service
public class GlobalConfigServiceImpl implements GlobalConfigService{
	
	@Resource
	private GlobalConfigMapper globalConfigMapper;
	
	@Resource
	private ConfigSystemCache configSystemCache;
	
	@Resource
	private InviteCodeService inviteCodeService;
	
	@Resource
	private TaskIntegralService taskIntegralService;
	
	@Resource
	private UserInfoService userInfoService;
	
	@Resource
	private SupportMccService supportMccService;

	//获取静态配置列表
	public  Result getGlobalConfirList(){
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("status", Const.Y);
		params.put("category", Const.GLOBAL_CATEGORY);
		
		List<GlobalConfig> entitys = globalConfigMapper.list(params);
		
		Map<String,Object> results = new HashMap<String,Object>();
		
		if(entitys!=null && !entitys.isEmpty()){
			
			for(GlobalConfig g:entitys){
				
				if (g.getDataType()!=null) {
					if(g.getDataType().equals(Const.GLOBAL_STRING_DATA_TYPE)){
						results.put(g.getKey(), g.getValue());
					}else if(g.getDataType().equals(Const.GLOBAL_INT_DATA_TYPE)){
						results.put(g.getKey(), Integer.valueOf(g.getValue()));
					}else if(g.getDataType().equals(Const.GLOBAL_ARRAY_DATA_TYPE)){
						results.put(g.getKey(), g.getValue().split(","));
					}else if(g.getDataType().equals(Const.GLOBAL_JSON_OBJECT_DATA_TYPE)){
						
						if (!StringUtil.isEmpty(g.getValue())) {
							results.put(g.getKey(), JSONUtil.stringToObject(g.getValue()));
						}else {
							results.put(g.getKey(), "");
						}
					}
					else if(g.getDataType().equals(Const.GLOBAL_JSON_ARRAY_DATA_TYPE)){
						if (!StringUtil.isEmpty(g.getValue())) {
							results.put(g.getKey(), JSONUtil.stringToArray(g.getValue()));
						}else {
							results.put(g.getKey(), "");
						}
					}else {
						results.put(g.getKey(), g.getValue());
					}
				}else {
					results.put(g.getKey(), g.getValue());
				}
				
				
				
			}
			
		}
		return new Result(results);
	}
	
	//获取个人中心功能列表
	@Override
	public	Result getPersonCenterList(Integer user_id,String ip,String lang){
		
		
		UserInfo userInfo = userInfoService.getUserForId(user_id);
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("status", Const.Y);
		params.put("category", Const.GLOBAL_PERSON_CENTER);
		params.put("luckyCredit", Const.TASK_LUCKY_CREDIT);
		params.put("categorys", new String[]{Const.TASK_CHECK_IN,Const.TASK_LUCKY_CREDIT});
		params.put("lang",lang);
		List<GlobalConfig> entitys = globalConfigMapper.getPersonCenterList(params);
		List<Map<String,Object>> reList = new ArrayList<Map<String,Object>>();
		if(entitys!=null && !entitys.isEmpty()){
			
			for(GlobalConfig g:entitys){
				
				if (g.getKey().equals(Const.TASK_BIND_PHONE) && userInfo!=null &&userInfo.getPhone()!=null) {//判断是否已绑定手机
					continue;
				}
				
				Map<String,Object> itemMap = new HashMap<String, Object>();
				itemMap.put("id",g.getId());
				itemMap.put("name",g.getName());
				itemMap.put("icon",g.getImagePic());
				itemMap.put("key",g.getKey());
				
				if (g.getKey().equals(Const.TASK_CHECK_IN)) { //签到单独处理
					String memo = taskIntegralService.todayCheckInIntegral(user_id);
					if (!StringUtil.isEmpty(memo)) {
						itemMap.put("button_text",memo);
					}else {
						itemMap.put("button_text",g.getValue());
					}
				}else if (g.getKey().equals(Const.TASK_BIND_PHONE) || g.getKey().equals(Const.TASK_VIDEO)|| g.getKey().equals(Const.TASK_INVITE)) { //绑定手机
					
					SupportMcc	supportmcc=supportMccService.getSupportCategoryIntegral(g.getKey(), ip);
					if (supportmcc!=null) {
						itemMap.put("button_text","+"+String.valueOf(supportmcc.getIntegral()));
					}else {
						itemMap.put("button_text",g.getValue());
					}
				}
				else {
					itemMap.put("button_text",g.getValue());
				}
				reList.add(itemMap);
			}
		}
		
		return new Result(reList,lang);
	}
	
	@Override
	public List<GlobalConfig> getGlobalConfigForCategory(String category){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("status", Const.Y);
		params.put("category", category);
		List<GlobalConfig> entitys = globalConfigMapper.list(params);
		return entitys;
	}
	
	@Override
	public GlobalConfig getConfigForKey(String category,String key){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("category", category);
		params.put("key", key);
		return globalConfigMapper.getOne(params);
	}
	//获取分享内容
	@Override
	public Result getFBShare(Integer user_id,String lang){
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("category", Const.GLOBAL_SHARE_CATEGORY);
		params.put("status", Const.Y);
		params.put("lang", lang);
		List<GlobalConfig> list = globalConfigMapper.getShareList(params);
		
		String code = inviteCodeService.getUserInviteCode(user_id);
		Map<String,Object> rsMap = new HashMap<String,Object>();
		if (list!=null && !list.isEmpty()) {
			
			for (GlobalConfig globalConfig : list) {
				if (globalConfig.getKey().equals(Const.SYSTEM_SHARE_TITLE)) {
					rsMap.put("title",globalConfig.getValue());
				}else if (globalConfig.getKey().equals(Const.SYSTEM_SHARE_CONTENT)) {
					String share_content=String.format(globalConfig.getValue(), code);
					rsMap.put("content",share_content);
				}else if (globalConfig.getKey().equals(Const.SYSTEM_SHARE_LINK)) {
					rsMap.put("link",globalConfig.getValue());
				}else if (globalConfig.getKey().equals(Const.SYSTEM_SHARE_IMAGE)) {
					rsMap.put("image",globalConfig.getValue());
				}
			}
			
			return new Result(rsMap,lang);
		}
		return new Result(ResultEnum.FAILED,lang);
		
	}
}
